package com.way.test;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Slf4j
public class Reflect {

    private static final Class<Student> cls = Student.class;

    public static void main(String[] args) throws Exception{
        getMethod(cls);
        getField(cls);
    }

    public static <T> void getMethod(Class<T> cls) throws Exception{
        Method[] methods = cls.getDeclaredMethods();
        for(Method m : methods) {
            log.info("getDeclaredMethods:{}",m.getName());
        }

        Method method = cls.getDeclaredMethod("getStudent",String.class,String.class);
        log.info("getDeclaredMethod:{}",method.getName());

        Method[] ms = cls.getMethods();
        for(Method m : ms) {
            log.info("getMethods:{}",m.getName());
        }

        Method m = cls.getMethod("getId",null);
        log.info("getMethod:{}",m.getName());
        log.info("getMethodReturnType:{}",m.getAnnotatedReturnType().getType());
    }

    public static <T> void getField(Class<T> cls) throws Exception{
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            log.info("getDeclaredFields:{}",field.getName());
        }

        Field field = cls.getDeclaredField("girFriendName");
        log.info("getDeclaredField:{}",field.getName());
        log.info("fieldValue:{}",field.get(cls.newInstance()));

        Field[] clsFields = cls.getFields();
        for (Field f : clsFields) {
            log.info("getFields:{}",f.getName());
        }

        Field f = cls.getField("girFriendName");
        log.info("getField:{}",f.getName());
    }
}
